package com.yune.adapter;
/*
* 加载歌曲列表的适配器*/

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.yune.R;
import com.yune.bean.SongBean;
import com.yune.utils.Network;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class SongListAdapter extends ArrayAdapter<SongBean> {
    private static final int layoutId = R.layout.item_song;//item_song歌曲在加载歌曲列表的适配器中的显示方式
    public SongListAdapter(Context context, List<SongBean> objects){
        super(context,layoutId,objects);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent){//convertView转换视图，ViewGroup视图组
        SongBean item=getItem(position);
        View view;
        ViewHolder viewHolder;
        if (convertView==null){
            view=LayoutInflater.from(getContext()).inflate(layoutId,parent,false);
            viewHolder= new ViewHolder();
            viewHolder.title=view.findViewById(R.id.find_item_title);
            viewHolder.author=view.findViewById(R.id.find_item_author);
            viewHolder.img=view.findViewById(R.id.find_item_img);
            view.setTag(viewHolder);//tag标签
        } else{
            view=convertView;
            viewHolder=(ViewHolder) view.getTag();
        }
        viewHolder.title.setText(item.songName);
        viewHolder.author.setText(item.singer);
        // Display显示器
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.loading)// 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.loaderror)// 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.loaderror)// 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
                .cacheOnDisk(false)// 设置下载的图片是否缓存在SD卡中
              //displayer显示器，Rounded圆形Bitmap位图Displayer显示器，cornerRadiusPixels角半径像素
                .displayer(new RoundedBitmapDisplayer(20))// 设置成圆角图片
                .build();
        ImageLoader.getInstance().displayImage(item.picture,viewHolder.img,options);
        Network.callApi("https://autumnfish.cn/song/url?id=" + item.songId, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {}
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                try {//assert断言、声称
                    assert response.body() != null;
                    item.songUrl=new JSONObject(response.body().string()).getJSONArray("data").getJSONObject(0).getString("url");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        return view;
    }
    static class ViewHolder{
        ImageView img;
        TextView title,author;
    }
}